package event

import (
	"github.com/Unixeno/TheMoon/planet/orm"
	"github.com/Unixeno/TheMoon/planet/orm/model"
	"github.com/jinzhu/gorm"
)

type Dao struct {
	model.Event
	db *gorm.DB
}

func New() Dao {
	return Dao{
		db: orm.DB,
	}
}

func (m *Dao) Create() error {
	return m.db.Create(&m.Event).Error
}

func (m *Dao) List(nums int) (events []model.Event, err error) {
	if nums == 0 {
		err = m.db.Debug().Model(&m.Event).Where(&m.Event).Where("level >= ?", m.Level).Order("time desc").Find(&events).Error
	} else {
		err = m.db.Debug().Model(&m.Event).Where(&m.Event).Where("level >= ?", m.Level).Order("time desc").Limit(nums).Find(&events).Error
	}
	return
}

// 获取节点zone下最后一个错误
func (m *Dao) GetLastError() (event *model.Event, err error) {
	err = m.db.Model(&m.Event).Where(&m.Event).Order("time desc").Take(event).Error
	return
}
